using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace TheLogWatcher
{
    public partial class MainWindow : Form
    {
        //private void ChangeColumnWidth(int column)
        //{
        //    string name = this.dataAccess.CurrentLogFormat.Name;
        //    Dictionary<string, int[]> widths = des
        //    Dictionary<string, bool[]> visibilities = sets.ColumnVisibility;
        //    if (!visibilities.ContainsKey(name) || visibilities[name] == null || ((bool[])visibilities[name]).Length <= column)
        //    {
        //        visibilities[name] = new bool[listView.Columns.Count];
        //    }
        //    if (listView.Columns[column].Width == 0)
        //    {
        //        if (widths != null && widths[name] != null && ((int[])widths[name]).Length > column)
        //        {
        //            listView.Columns[column].Width = ((int[])widths[name])[column];
        //        }
        //        else
        //        {
        //            // Old width cannot be restored, use standard value
        //            // TODO: implement standard column widths for each format and use it here
        //            listView.Columns[column].Width = 200;
        //        }
                
        //        ((bool[])visibilities[name])[column] = true;
        //    }
        //    else
        //    {
        //        widths[column] = listView.Columns[column].Width;
        //        listView.Columns[column].Width = 0;
        //        ((bool[])visibilities[name])[column] = false;
        //    }
        //}
        
        /// <summary>
        ///     Clears the listview and fills it with the loaded data.
        ///     (Is not used when "Load Active Rows" is activated)
        /// </summary>
        private void PopulateListView()
        {
            listView.Items.Clear();
            for (int j = 0; j < MessageType.Length(); j++)
            {
                if (sets.SelectedTypes.Contains(MessageType.StringArray()[j]))
                {
                    List<LogEntry> entries = this.getData(j);
                    if (entries != null)
                    {
                        for (int i = 0; i < entries.Count; i++)
                        {
                            ListViewItem item = Utilities.getListViewItem(entries[i], this.dataAccess.CurrentLogFormat);
                            item.Tag = i;
                            listView.Items.Add(item);
                        }
                    }
                }
            }

        }
        
        /// <summary>
        ///     Creates a context menu for the column headers of
        ///     the listview. This is needed to enable or disable
        ///     the visibility of each column.
        /// </summary>
        private void CreateListViewContextMenuItems()
        {
            if (this.dataAccess.CurrentLogFormat != null)
            {
                contextMenuListView.Items.Clear();
                for (int i = 0; i < listView.Columns.Count; i++)
                {
                    ToolStripMenuItem item = new ToolStripMenuItem(listView.Columns[i].Text);
                    item.Tag = i;
                    item.Checked = this.colMan.isVisible(this.dataAccess.CurrentLogFormat.Name, i);
                    item.Click += new EventHandler(contextMenuItem_Click);
                    contextMenuListView.Items.Add(item);
                }
            }
        }

        private bool formLoaded = false;

        private void listView_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            if (this.formLoaded && this.colMan != null)
            {
                this.colMan.changedWidth(e.ColumnIndex);
                CreateListViewContextMenuItems();
            }
        }
    }
}
